/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.recordsCenter.service.impl;

import me.zhengjie.modules.his.domain.RegisteredType;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.recordsCenter.repository.RegisteredTypeRepository;
import me.zhengjie.modules.his.recordsCenter.service.RegisteredTypeService;
import me.zhengjie.modules.his.recordsCenter.service.dto.RegisteredTypeDto;
import me.zhengjie.modules.his.recordsCenter.service.dto.RegisteredTypeQueryCriteria;
import me.zhengjie.modules.his.recordsCenter.service.mapstruct.RegisteredTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class RegisteredTypeServiceImpl implements RegisteredTypeService {

    private final RegisteredTypeRepository registeredTypeRepository;
    private final RegisteredTypeMapper registeredTypeMapper;

    @Override
    public Map<String,Object> queryAll(RegisteredTypeQueryCriteria criteria, Pageable pageable){
        Page<RegisteredType> page = registeredTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(registeredTypeMapper::toDto));
    }

    @Override
    public List<RegisteredTypeDto> queryAll(RegisteredTypeQueryCriteria criteria){
        return registeredTypeMapper.toDto(registeredTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RegisteredTypeDto findById(Integer registeredTypeId) {
        RegisteredType registeredType = registeredTypeRepository.findById(registeredTypeId).orElseGet(RegisteredType::new);
        ValidationUtil.isNull(registeredType.getRegisteredTypeId(),"RegisteredType","registeredTypeId",registeredTypeId);
        return registeredTypeMapper.toDto(registeredType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RegisteredTypeDto create(RegisteredType resources) {
        return registeredTypeMapper.toDto(registeredTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RegisteredType resources) {
        RegisteredType registeredType = registeredTypeRepository.findById(resources.getRegisteredTypeId()).orElseGet(RegisteredType::new);
        ValidationUtil.isNull( registeredType.getRegisteredTypeId(),"RegisteredType","id",resources.getRegisteredTypeId());
        registeredType.copy(resources);
        registeredTypeRepository.save(registeredType);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer registeredTypeId : ids) {
            registeredTypeRepository.deleteById(registeredTypeId);
        }
    }

    @Override
    public void download(List<RegisteredTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RegisteredTypeDto registeredType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("类型", registeredType.getType());
            map.put("挂号价格", registeredType.getPrice());
            map.put("创建者", registeredType.getCreateBy());
            map.put("更新者", registeredType.getUpdateBy());
            map.put("创建日期", registeredType.getCreateTime());
            map.put("更新时间", registeredType.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}